package cn.com.yx.ms.controller;

import cn.com.yx.ms.application.web.ApiPager;
import cn.com.yx.ms.application.web.ApiQuery;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.dbmodel.SysRole;
import cn.com.yx.ms.domain.sys.FunModel;
import cn.com.yx.ms.service.ISysService;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.view.ApiResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * 应用中心
 */
@RestController
@RequestMapping(value = "/")
@Api(value = "角色", description = "角色资源获取")
public class RoleController {

    private static final Logger logger = LoggerFactory.getLogger(DeptController.class);

    @Resource
    private MessageSource messageSource;

    @Resource
    private ISysService iSysService;


    @PostMapping("role")
    @ApiOperation(value = "创建角色信息", notes = "创建juese信息")
    @ApiImplicitParam(paramType = "body", name = "userModel", dataType = "Map", required = true, value = "用户实体详细信息")
    public ApiResult create(@RequestBody @Validated SysRole sysRole, BindingResult bindingResult) throws Exception {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", iSysService.saveRole(sysRole));

    }

    @DeleteMapping("roles/{id}")
    @ApiOperation(value = "删除角色信息", notes = "删除角色信息")
    @ApiImplicitParam(paramType = "path", name = "id", dataType = "String", required = true, value = "角色id")
    public ApiResult delete(@PathVariable("id") String id) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iSysService.deleteRole(id));
    }

    @PutMapping("role")
    @ApiOperation(value = "更新角色信息", notes = "更新角色信息")
    @ApiImplicitParam(paramType = "body", name = "sysRole", dataType = "Map", required = true, value = "角色实体详细信息")
    public ApiResult updateModel(@RequestBody @Validated SysRole sysRole, BindingResult bindingResult) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", iSysService.saveRole(sysRole));
    }

    @PatchMapping("role")
    @ApiOperation(value = "角色信息部分更新", notes = "角色信息部分更新")
    @ApiImplicitParam(paramType = "body", name = "sysRole", dataType = "Map", required = true, value = "角色信息")
    public ApiResult updateProperty(@RequestBody @Validated SysRole sysRole, BindingResult bindingResult) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", iSysService.saveRole(sysRole));
    }

    @ResponseBody
    @RequestMapping(value = "roles", method = RequestMethod.GET)
    @ApiOperation("获取所有角色信息")
    public ApiResult find(@ApiQuery ApiQueryBase query, HttpServletRequest request) {
        query.getConditions().put("rolename", request.getParameter("rolename"));
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iSysService.findRoles(query));
    }

    @ResponseBody
    @RequestMapping(value = "roles/page", method = RequestMethod.GET)
    @ApiOperation("获取带分页的角色信息")
    public ApiResult findPage(@ApiQuery ApiQueryBase query, HttpServletRequest request) {

        Object obj = request.getSession().getAttribute("userprofile");
        // 查询条件接受
        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));
        query.getConditions().put("rolename", request.getParameter("rolename"));

        // 翻页对象构建
        PageHelper.startPage(query.getPi(), query.getPs(), true);
        Page<SysRole> page = (Page<SysRole>) iSysService.findRoles(query);
        ApiPager<SysRole> apiPager = new ApiPager<SysRole>(query.getPs(),
                query.getPi(),
                page.getTotal(),
                page.getResult());

        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", apiPager);

    }

    @ResponseBody
    @RequestMapping(value = "roles/{id}", method = GET)
    @ApiImplicitParam(paramType = "String", name = "id", dataType = "String", required = true, value = "角色ID")
    @ApiOperation("获取单个角色信息")
    public ApiResult findById(@PathVariable("id") String id) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iSysService.findOneRole(id));

    }

    @ResponseBody
    @RequestMapping(value = "roles/{roleid}/funs", method = GET)
    @ApiImplicitParam(paramType = "String", name = "id", dataType = "String", required = true, value = "角色ID")
    @ApiOperation("获取单个角色信息下的功能菜单集合")
    public ApiResult findFunsByRoleId(@PathVariable("roleid") String roleid) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iSysService.findAllFunsByRoleId(roleid));
    }

    @PostMapping("roles/{roleid}/funs")
    @ApiOperation(value = "保存权限信息", notes = "保存权限信息")
    @ApiImplicitParam(paramType = "body", name = "funModels", dataType = "Map", required = true, value = "权限详细信息")
    public ApiResult create(@PathVariable("roleid") String roleid, @RequestBody List<FunModel> funModels, BindingResult bindingResult) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", iSysService.saveFuns(roleid, funModels));

    }


}